package com.cqyc.imview

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.JavascriptInterface
import com.cqyc.imview.TUIStartUtils.Companion.startC2CChatActivity
import com.cqyc.imview.activity.ConversationActivity
import com.cqyc.imview.activity.MWebActivity
import com.cqyc.imview.model.MWebFModel
import com.cqyc.network.http.DataCallBack
import com.cqyc.network.http.HttpUtil
import com.cqyc.network.utils.ToastUtils
import com.tencent.qcloud.tuicore.TUICore
import com.tencent.qcloud.tuikit.tuichat.model.GoodsModel
import com.tencent.smtt.sdk.WebView
import org.json.JSONObject

class JavascriptInterfaceUtils(view: WebView, model: MWebFModel) {
    private val webView = view
    private val context = webView.context
    private val viewModel = model

    @JavascriptInterface
    fun goBack(o: Any?) {
        Log.e("Javascript", "goBack")
        Handler(Looper.getMainLooper()).post {
            viewModel.back()
        }
    }

    //打开新的activity
    @JavascriptInterface
    fun openWebPage(any: String) {
        Log.e("openWebPage", "--->$any")
        val jsObj=JSONObject(any)
        val url = jsObj.getString("url")
        val showNav = jsObj.getBoolean("showNav")
        val title = jsObj.getString("title")
        Handler(Looper.getMainLooper()).post {
            val intent = Intent(context, MWebActivity::class.java)
            intent.putExtra(MWebActivity.TITLE, title)
            intent.putExtra(MWebActivity.TITLEVISIBLE, showNav)
            intent.putExtra(MWebActivity.URL, url)
            context.startActivity(intent)
        }
    }

    //打开会话列表activity
    @JavascriptInterface
    fun openConversationList() {
        Handler(Looper.getMainLooper()).post {
            val intent = Intent(context, ConversationActivity::class.java)
            context.startActivity(intent)
        }
    }

    //跳转设置界面
    @JavascriptInterface
    fun jumpNativePage(any: Any?) {
        Handler(Looper.getMainLooper()).post {
            val bundle = Bundle()
            TUICore.startActivity("SettingActivity", bundle)
        }
    }

    //获取未读消息数
    @JavascriptInterface
    fun getUnReadMsgNumber() {
        Handler(Looper.getMainLooper()).post {
            putUnReadMsgNumber(webView, 89)
        }
    }

    private fun putUnReadMsgNumber(webView: WebView, number: Int) {
        webView.loadUrl("javascript:getUnReadMsgNumber($number)")
    }

    //打开单聊
    @JavascriptInterface
    fun openChat(str: String?) {
        //店铺id 店铺名称 店铺头像
        Log.e("openChat", "----$str  --  ")
        //{"goodID":160589,"userID":10000003,"merchantName":"烟雨朦胧"}
        Handler(Looper.getMainLooper()).post {
            if (str == null || "" == str) {
                ToastUtils.showLong("数据异常")
                return@post
            }
            val jsonObject = JSONObject(str)
            val userID: String = jsonObject.getString("userID")
            if (jsonObject.has("goodID")) {
                val goodID: String = jsonObject.getString("goodID")
                val merchantName: String = jsonObject.getString("merchantName")
                HttpUtil.api.findGoods(goodID)
                    .enqueue(DataCallBack(onResponse = {
                        val info = it
                        val goodsModel = GoodsModel()
                        goodsModel.item_img = info.itemImg
                        goodsModel.item_name = info.itemName
                        goodsModel.sales_price = info.salesPrice
                        goodsModel.web_url = info.webUrl
                        goodsModel.itemStatus = info.itemStatus
                        startC2CChatActivity(
                            userID,
                            merchantName,
                            "",
                            goodsModel
                        )
                    }, onFailure = {
                        ToastUtils.showLong("获取商品信息异常")
                    }))
            } else {
                HttpUtil.api.findMchInfo(userID).enqueue(DataCallBack(onResponse = {
                    startC2CChatActivity(
                        userID,
                        it.mchName,
                        it.mchImg,
                        null
                    )
                }, onFailure = {
                    ToastUtils.showLong("获取店铺异常")
                }))
            }
        }
    }
}